package com.reggie.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.reggie.mapper.CategoryMapper;
import com.reggie.pojo.Category;
import com.reggie.service.CategoryService;
import com.reggie.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService{
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public PageUtil<Category> getCategoryList(Integer page, Integer pageSize) {
        PageUtil<Category> categoryPageUtil = new PageUtil<>();
        PageHelper.startPage(page, pageSize);

        List<Category> categoryList = categoryMapper.findAll(page, pageSize);
        if (categoryList==null) return null;

        Page<Category> categoryPage = (Page<Category>) categoryList;
        categoryPageUtil.setTotal(categoryPage.getTotal());
        categoryPageUtil.setRecords(categoryPage.getResult());
        return categoryPageUtil;
    }

    @Override
    public Category getCategoryById(Long id) {
        Category category =  categoryMapper.findById(id);
        if (category==null) return null;
        return category;
    }

    @Override
    public boolean delete(Long id) {
        if (categoryMapper.delete(id)==1) return true;
        return false;
    }

    @Override
    public boolean edit(Category category) {
        if (categoryMapper.edit(category)==1) return true;
        return false;
    }

    @Override
    public boolean add(Category category) {
        if (categoryMapper.add(category)==1) return true;
        return false;
    }

    @Override
    public List<Category> getCategoryByType(Integer type) {
        List<Category> categoryList = categoryMapper.findByType(type);
        if (categoryList==null) return null;
        return categoryList;
    }
}
